package com.examine.action;


import com.alibaba.fastjson.JSON;
import com.examine.bean.AccessApplication;
import com.examine.bean.MyApproval;
import com.examine.bean.PageResult;
import com.examine.bean.UserReplenish;
import com.examine.serviceImpl.AccessApplicationServiceImpl;
import com.examine.serviceImpl.MyApprovalServiceImpl;
import com.examine.serviceImpl.UserReplenishServiceImpl;
import com.examine.utils.DateUtil;
import com.examine.utils.StrResponse;
import com.examine.utils.UuidUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;

public class AccessApplicationJsonAction extends ActionSupport {
    private LinkedHashMap mymp;
    private AccessApplicationServiceImpl applicationService;
    private MyApprovalServiceImpl myApprovalService;
    private AccessApplication application;
    private UserReplenishServiceImpl userReplenishService;

    public LinkedHashMap getModelMap() {
        return mymp;
    }

    public void setModelMap(LinkedHashMap mymp) {
        this.mymp = mymp;
    }


    public String addAccessApplication() {
        application = JSON.parseObject(StrResponse.getStrResponse(), AccessApplication.class);

        String id = UuidUtil.getUUID();
        application.setId(id);
        application.setCreationTime(DateUtil.getCurrentTime());

        application.getMyApproval().setId(application.getId());
        application.getMyApproval().setApprovalNoteId(application.getScholasticId());
        application.getMyApproval().setCreationTime(application.getCreationTime());

        applicationService = new AccessApplicationServiceImpl();
        myApprovalService = new MyApprovalServiceImpl();

        int count = applicationService.insertSelective(application);
        int count1 = myApprovalService.insertSelective(application.getMyApproval());


        if (count == 0 && count1 == 0) {
            mymp = StrResponse.error("添加假条失败");
            return ERROR;
        }

        mymp = StrResponse.success("添加假条成功");
        return SUCCESS;
    }

    public String queryApplication() {
        application = JSON.parseObject(StrResponse.getStrResponse(), AccessApplication.class);

        if(StringUtils.isEmpty(application.getId())){
            mymp = StrResponse.error("假条ID不能为空");
            return ERROR;
        }

        if(application.getMyApproval() == null){
            mymp = StrResponse.error("用户类型不能为空");
            return ERROR;
        }

        applicationService = new AccessApplicationServiceImpl();
        AccessApplication application1 = applicationService.selectByPrimaryKey(application.getId());

        if (application1 == null){
            mymp = StrResponse.error("查询假条失败");
            return ERROR;
        }

        myApprovalService = new MyApprovalServiceImpl();
        userReplenishService = new UserReplenishServiceImpl();

        UserReplenish userReplenish = userReplenishService.selectByPrimaryKey(application1.getScholasticId());

        List<MyApproval> myApproval1 = myApprovalService.shareQueryMyApproval(application.getMyApproval());

        application1.setUserReplenish(userReplenish);

        if (myApproval1.size() != 0){
            application1.setMyApprovalList(myApproval1);
        }

        mymp = StrResponse.success("查询假条成功", application1);
        return SUCCESS;
    }

    public String shareQueryApplication() {
        //获取前端传输的包装的数据
        application = JSON.parseObject(StrResponse.getStrResponse(), AccessApplication.class);
        //判断用户传入的审核信息补充数据字段是否为空
        if(application.getMyApproval() != null){
            //根据用户的类型，分别判断用户的字段id是否为空
            if ("1".equals(application.getMyApproval().getUserLevel())){
                if(StringUtils.isEmpty(application.getCounsellorId())){
                    mymp = StrResponse.error("教师ID不能为空");
                    return ERROR;
                }
            }else if ("2".equals(application.getMyApproval().getUserLevel())){
                if(StringUtils.isEmpty(application.getScholasticId())){
                    mymp = StrResponse.error("学生ID不能为空");
                    return ERROR;
                }
            }else if ("3".equals(application.getMyApproval().getUserLevel())){
                if(StringUtils.isEmpty(application.getCollegeLeadersId())){
                    mymp = StrResponse.error("院领导ID不能为空");
                    return ERROR;
                }
            }else if ("4".equals(application.getMyApproval().getUserLevel())){
                if(StringUtils.isEmpty(application.getSchoolLeaderId())){
                    mymp = StrResponse.error("校领导ID不能为空");
                    return ERROR;
                }
            }else if ("5".equals(application.getMyApproval().getUserLevel())){
                if(StringUtils.isEmpty(application.getMyApproval().getApprovalNoteId())){
                    mymp = StrResponse.error("查询人ID不能为空");
                    return ERROR;
                }
            }else {
                mymp = StrResponse.error("未知用户，查询失败");
                return ERROR;
            }
        }else {
            mymp = StrResponse.error("查询类型不能为空");
            return ERROR;
        }

        //查询出符合用户要求的数据集信息
        applicationService = new AccessApplicationServiceImpl();
        List<AccessApplication> applicationList = applicationService.shareQueryApplication(application);

        //判断是否有数据
        if (applicationList.size() == 0 ){
            mymp = StrResponse.error("查询假条失败");
            return ERROR;
        }

        myApprovalService = new MyApprovalServiceImpl();
        userReplenishService = new UserReplenishServiceImpl();

        //获取到每条数据的审核补充信息和用户补充资料
        for (int i = 0; i < applicationList.size(); i++){
            //实例化审核补充信息
            MyApproval myApproval = new MyApproval();
            applicationList.get(i).setMyApproval(myApproval);

            //实例化用户补充资料
            UserReplenish userReplenish = userReplenishService.selectByPrimaryKey(applicationList.get(i).getScholasticId());

            applicationList.get(i).getMyApproval().setId(applicationList.get(i).getId());
            applicationList.get(i).getMyApproval().setApprovalStatus(applicationList.get(i).getAccessStatus());

            //获取到补充的审核信息数据集
            List<MyApproval> myApproval1 = myApprovalService.shareQueryMyApproval(applicationList.get(i).getMyApproval());

            applicationList.get(i).setUserReplenish(userReplenish);

            //判断补充审核的数据是否有数据
            if (myApproval1.size() != 0){
                applicationList.get(i).setMyApproval(myApproval1.get(0));
            }


        }

        //查询符合条件的数据有多少条
        int countLike = applicationService.queryAccessCountLike(application);

        //实例化分页组装数据
        PageResult pageResult = new PageResult();
        pageResult.setCurrentPage(application.getPageResult().getCurrentPage());
        pageResult.setPageSize(application.getPageResult().getPageSize());
        pageResult.setTotalSize(countLike);
        pageResult.setItems(applicationList);

        //返回包装的数据
        mymp = StrResponse.success("查询假条成功", pageResult);
        return SUCCESS;
    }

    public String updateApplication() {
        application = JSON.parseObject(StrResponse.getStrResponse(), AccessApplication.class);

        if(StringUtils.isEmpty(application.getId())){
            mymp = StrResponse.error("假条ID不能为空");
            return ERROR;
        }

        if(application.getMyApproval() == null){
            mymp = StrResponse.error("审批信息不能为空");
            return ERROR;
        }

        if(StringUtils.isEmpty(application.getMyApproval().getId())){
            mymp = StrResponse.error("审批信息ID不能为空");
            return ERROR;
        }

        String time = DateUtil.getCurrentTime();
        application.setUpdateTime(time);

        applicationService = new AccessApplicationServiceImpl();
        myApprovalService = new MyApprovalServiceImpl();

        int count = applicationService.updateByPrimaryKeySelective(application);

        int count1 = 0;
        if ("u".equals(application.getMyApproval().getUpdateStatus())){
            application.getMyApproval().setUpdateTime(time);
            count1 = myApprovalService.updateSelective(application.getMyApproval());
        }else if ("a".equals(application.getMyApproval().getUpdateStatus())){
            application.getMyApproval().setCreationTime(time);
            count1 = myApprovalService.insertSelective(application.getMyApproval());
        }

        if (count == 0 && count1 == 0) {
            mymp = StrResponse.error("更新假条失败");
            return ERROR;
        }

        mymp = StrResponse.success("数据更新成功");
        return SUCCESS;
    }



}
